#!/bin/bash

remoteip=$1
if [ ! -n "$remoteip" ] ; then
    echo 'Usage: retry_ssh.sh xx.xx.xx.xx'
    exit 1
fi

pid=$(ss -tnp | grep "${remoteip}:22" | cut -d',' -f2)
if [ -n "$pid" ]; then
    echo 'Connection is alive, kill and reconnect.'
    kill -9 $pid
    ssh -f -R9898:localhost:22 infojhc.cn /home/luyanfei/scripts/idle.sh
else
    echo 'Connection does not exists, connect.'
    ssh -f -R9898:localhost:22 infojhc.cn /home/luyanfei/scripts/idle.sh
fi
